package com.hengxinguotong.hxgtwygsp.ui.supervise.patrol.issue;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.hengxinguotong.hxgtwygsp.R;
import com.hengxinguotong.hxgtwygsp.model.Year;
import com.hengxinguotong.hxgtwygsp.ui.base.BaseFragment;
import com.hengxinguotong.hxgtwygsp.widget.DropListPW;
import com.hengxinguotong.hxgtwygsp.widget.YearPW;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class PatrolIssueFragment extends BaseFragment implements PatrolIssueContract.IView, DropListPW.OnClickCallback<Year>
{
    @Inject
    PatrolIssueContract.IPresenter mPresenter;
    @BindView(R.id.issue_chart)
    BarChart issueChart;
    @BindView(R.id.issue_year)
    TextView issueYear;
    @BindView(R.id.patrol_issue_count)
    TextView patrolIssueCount;

    private List<Year> yearList;

    public PatrolIssueFragment()
    {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        mPresenter.bindView(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mPresenter.dropView();
    }

    @Override
    public int getLayoutResId()
    {
        return R.layout.fragment_patrol_issue;
    }

    @Override
    public void init()
    {
        yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        issueYear.setText(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
        Year year;
        for (int i = 0; i < 3; i++)
        {
            year = new Year();
            year.setName(getString(R.string.patrol_year, calendar.get(Calendar.YEAR)));
            yearList.add(year);
            calendar.add(Calendar.YEAR, -1);
        }

        // TODO: 模拟数据
        patrolIssueCount.setText(String.format(getString(R.string.patrol_issue_count),39));

        String[] quarter = {"第一季度", "第二季度", "第三季度", "第四季度"};
        int[] valuesY = {17, 22, 0, 0};
        int textColor = ContextCompat.getColor(getContext(), R.color.cC8D8FF);
        float dp = getResources().getDimension(R.dimen.px_1);

        issueChart.getLegend().setEnabled(false);
        issueChart.setScaleEnabled(false);
        issueChart.setDragEnabled(false);
        issueChart.setTouchEnabled(false);
        issueChart.setExtraOffsets(30 * dp, 0, 50 * dp, 5 * dp);
        issueChart.setDrawBarShadow(false);
        issueChart.setDrawValueAboveBar(true);
        issueChart.getDescription().setEnabled(false);
        issueChart.setMaxVisibleValueCount(60);
        issueChart.setPinchZoom(false);
        issueChart.setDrawGridBackground(false);


        XAxis xAxis = issueChart.getXAxis();
        xAxis.setYOffset(20 * dp);
        xAxis.setAvoidFirstLastClipping(false);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(4);
        xAxis.setTextColor(textColor);
        xAxis.setTextSize(dp * 28f);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(quarter));

        YAxis leftAxis = issueChart.getAxisLeft();
        YAxis rightAxis = issueChart.getAxisRight();
        rightAxis.setEnabled(false);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setTextColor(textColor);
        leftAxis.setTextSize(dp * 24f);
        leftAxis.setLabelCount(5);
        leftAxis.setSpaceTop(15f);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setGridColor(Color.parseColor("#414C7B"));
        leftAxis.setAxisMinimum(0);

        ArrayList<BarEntry> barValues = new ArrayList<>();
        for (int i = 0; i < valuesY.length; i++) {
            barValues.add(new BarEntry(i,valuesY[i]));
        }
        BarDataSet barDataSet = new BarDataSet(barValues, "test");
        barDataSet.setDrawIcons(false);
        barDataSet.setColor(Color.parseColor("#0099FF"));
        barDataSet.setValueTextColor(textColor);
        barDataSet.setValueTextSize(10*dp);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(barDataSet);
        BarData data = new BarData(dataSets);
        data.setBarWidth(0.15f);
        //设置柱状上方数值的格式
        data.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float v) {
                String str = v + "";
                if (str.length()==0) {
                    return str;
                }
                return str.substring(0, str.indexOf("."));//设置自己的返回位数
            }
        });

        issueChart.setData(data);
    }

    @OnClick(R.id.issue_year_container)
    public void onViewClicked(View view)
    {
        YearPW yearPW = new YearPW(getContext(), view.getWidth());
        yearPW.setOnClickCallback(this);
        yearPW.initData(yearList, 0);
        yearPW.showAsDropDown(view);
    }

    @Override
    public void onClickPW(Year year)
    {
        issueYear.setText(year.getName());
    }
}
